package com.zenithmind.chat.pojo.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.time.LocalDateTime;

/**
 * 私聊会话用户关系VO - 遵循单一职责原则
 * 只负责私聊会话用户关系信息的视图展示
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@Schema(description = "私聊会话用户关系VO")
public class ChatPrivateSessionUserVO {

    @Schema(description = "关系ID")
    private String id;

    @Schema(description = "会话ID")
    private String sessionId;

    @Schema(description = "用户ID")
    private String userId;

    @Schema(description = "用户名")
    private String username;

    @Schema(description = "用户昵称")
    private String nickname;

    @Schema(description = "用户头像")
    private String avatar;

    @Schema(description = "最后访问时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime lastAccessTime;

    @Schema(description = "最后阅读时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime lastReadTime;

    @Schema(description = "未读消息数")
    private Integer unreadCount;

    @Schema(description = "是否置顶")
    private Boolean isTop;

    @Schema(description = "是否免打扰")
    private Boolean isMuted;

    @Schema(description = "是否归档")
    private Boolean isArchived;

    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    @Schema(description = "会话名称")
    private String sessionName;

    @Schema(description = "对方用户ID")
    private String otherUserId;

    @Schema(description = "对方用户名")
    private String otherUsername;

    @Schema(description = "对方用户昵称")
    private String otherNickname;

    @Schema(description = "对方用户头像")
    private String otherAvatar;

    @Schema(description = "对方在线状态")
    private Integer otherOnlineStatus;

    @Schema(description = "最后一条消息内容")
    private String lastMessageContent;

    @Schema(description = "最后一条消息时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime lastMessageTime;

    @Schema(description = "最后一条消息类型")
    private Integer lastMessageType;

    /**
     * 获取显示的用户名称
     * 
     * @return 显示名称
     */
    public String getDisplayUserName() {
        if (nickname != null && !nickname.trim().isEmpty()) {
            return nickname;
        }
        return username;
    }

    /**
     * 获取显示的对方用户名称
     * 
     * @return 对方显示名称
     */
    public String getDisplayOtherUserName() {
        if (otherNickname != null && !otherNickname.trim().isEmpty()) {
            return otherNickname;
        }
        return otherUsername;
    }

    /**
     * 获取显示的用户头像
     * 
     * @return 显示头像
     */
    public String getDisplayAvatar() {
        if (avatar != null && !avatar.trim().isEmpty()) {
            return avatar;
        }
        return "/images/default-user-avatar.png";
    }

    /**
     * 获取显示的对方用户头像
     * 
     * @return 对方显示头像
     */
    public String getDisplayOtherAvatar() {
        if (otherAvatar != null && !otherAvatar.trim().isEmpty()) {
            return otherAvatar;
        }
        return "/images/default-user-avatar.png";
    }

    /**
     * 获取安全的未读消息数
     * 
     * @return 未读消息数
     */
    public Integer getSafeUnreadCount() {
        return unreadCount != null ? unreadCount : 0;
    }

    /**
     * 检查是否有未读消息
     * 
     * @return 是否有未读消息
     */
    public boolean hasUnreadMessages() {
        return getSafeUnreadCount() > 0;
    }

    /**
     * 检查是否置顶
     * 
     * @return 是否置顶
     */
    public boolean isTop() {
        return isTop != null && isTop;
    }

    /**
     * 检查是否免打扰
     * 
     * @return 是否免打扰
     */
    public boolean isMuted() {
        return isMuted != null && isMuted;
    }

    /**
     * 检查是否归档
     * 
     * @return 是否归档
     */
    public boolean isArchived() {
        return isArchived != null && isArchived;
    }

    /**
     * 检查对方是否在线
     * 
     * @return 对方是否在线
     */
    public boolean isOtherUserOnline() {
        return otherOnlineStatus != null && otherOnlineStatus == 1;
    }

    /**
     * 获取对方在线状态名称
     * 
     * @return 在线状态名称
     */
    public String getOtherOnlineStatusName() {
        if (otherOnlineStatus == null) {
            return "离线";
        }
        switch (otherOnlineStatus) {
            case 1:
                return "在线";
            case 2:
                return "忙碌";
            case 3:
                return "离开";
            case 4:
                return "隐身";
            default:
                return "离线";
        }
    }

    /**
     * 获取最后消息的显示内容
     * 
     * @return 最后消息显示内容
     */
    public String getDisplayLastMessage() {
        if (lastMessageContent == null || lastMessageContent.trim().isEmpty()) {
            return getLastMessageTypeDisplay();
        }
        
        // 限制显示长度
        if (lastMessageContent.length() > 50) {
            return lastMessageContent.substring(0, 50) + "...";
        }
        
        return lastMessageContent;
    }

    /**
     * 获取最后消息类型的显示文本
     * 
     * @return 消息类型显示文本
     */
    public String getLastMessageTypeDisplay() {
        if (lastMessageType == null) {
            return "[消息]";
        }
        switch (lastMessageType) {
            case 1:
                return "[文本]";
            case 2:
                return "[图片]";
            case 3:
                return "[语音]";
            case 4:
                return "[视频]";
            case 5:
                return "[文件]";
            case 6:
                return "[位置]";
            case 7:
                return "[链接]";
            default:
                return "[消息]";
        }
    }

    /**
     * 获取最后访问时间的相对时间
     * 
     * @return 相对时间描述
     */
    public String getLastAccessTimeRelative() {
        if (lastAccessTime == null) {
            return "从未访问";
        }
        return getRelativeTime(lastAccessTime);
    }

    /**
     * 获取最后消息时间的相对时间
     * 
     * @return 相对时间描述
     */
    public String getLastMessageTimeRelative() {
        if (lastMessageTime == null) {
            return "";
        }
        return getRelativeTime(lastMessageTime);
    }

    /**
     * 计算相对时间
     * 
     * @param time 时间
     * @return 相对时间描述
     */
    private String getRelativeTime(LocalDateTime time) {
        LocalDateTime now = LocalDateTime.now();
        long minutes = java.time.Duration.between(time, now).toMinutes();
        
        if (minutes < 1) {
            return "刚刚";
        } else if (minutes < 60) {
            return minutes + "分钟前";
        } else if (minutes < 1440) { // 24小时
            return (minutes / 60) + "小时前";
        } else if (minutes < 10080) { // 7天
            return (minutes / 1440) + "天前";
        } else {
            return time.format(java.time.format.DateTimeFormatter.ofPattern("MM-dd"));
        }
    }

    /**
     * 检查会话是否活跃
     * 
     * @return 是否活跃（最近7天有访问）
     */
    public boolean isActive() {
        if (lastAccessTime == null) {
            return false;
        }
        LocalDateTime sevenDaysAgo = LocalDateTime.now().minusDays(7);
        return lastAccessTime.isAfter(sevenDaysAgo);
    }

    /**
     * 获取会话状态标识
     * 
     * @return 状态标识
     */
    public String getSessionStatusBadge() {
        if (isTop()) {
            return "top";
        } else if (isMuted()) {
            return "muted";
        } else if (isArchived()) {
            return "archived";
        } else if (hasUnreadMessages()) {
            return "unread";
        } else {
            return "normal";
        }
    }

    /**
     * 获取会话状态名称
     * 
     * @return 状态名称
     */
    public String getSessionStatusName() {
        if (isTop()) {
            return "置顶";
        } else if (isMuted()) {
            return "免打扰";
        } else if (isArchived()) {
            return "已归档";
        } else if (hasUnreadMessages()) {
            return "有未读";
        } else {
            return "正常";
        }
    }

    /**
     * 获取未读消息数的显示文本
     * 
     * @return 未读消息数显示文本
     */
    public String getUnreadCountDisplay() {
        int count = getSafeUnreadCount();
        if (count == 0) {
            return "";
        } else if (count > 99) {
            return "99+";
        } else {
            return String.valueOf(count);
        }
    }
}
